<template>
  <div class="login-container">
    <!-- 视频背景 -->
    <!-- loop 当媒介文件完成播放后再次开始播放 -->
    <!-- autoplay 视频在就绪后马上播放 -->
    <!-- muted默认静音 -->
    <!-- 设置css 上下左右为0 fixed 不需要宽高百分百 -->
    <video src="" class="video" muted loop="loop" autoplay="true"></video>
    <!-- 背景图 -->
    <div class="bg2"></div>
    <!-- 常规登入 -->
    <el-form
      :model="loginForm"
      status-icon
      :rules="rules"
      ref="loginForm"
      label-width="100px"
      class="loginForm2"
    >
      <h1 v-text="title" class="title"></h1>
      <i
        :class="['jiaobiao', 'iconfont', jiaobiaoType]"
        @click="changeLoginType"
      ></i>
      <!-- 常规登入 -->
      <Common
        ref="commonLogin"
        @zdLoginFn="handlerLogin"
        @changeMessType="changeMessType"
        @submit="submit"
        v-if="loginType === 'common'"
      ></Common>

      <!-- 二维码登录 -->
      <Qr
        :storageUserInfo="storageUserInfo"
        v-else-if="loginType === 'qr'"
      ></Qr>

      <!-- 短信登录 -->
      <Mess :storageUserInfo="storageUserInfo" v-else></Mess>
    </el-form>
  </div>
</template>

<script>
import Common from "@/components/Login/commonLogin";
import Qr from "@/components/Login/qrLogin";
import Mess from "@/components/Login/messLogin";
import * as api from "@/api/users.js";
import storage from "@/utils/storages";
//校验用户名
var validateUsername = (rule, value, callback) => {
  //3-16位数字字母下划线
  var uPattern = /^[a-zA-Z0-9_-]{3,16}$/;
  //如果校验通过 直接调用callback()
  //如果校验不通过 callback()里面传参数
  if (uPattern.test(value)) {
    callback();
  } else {
    callback("3-16位数字字母下划线");
  }
};
//校验密码
var validatePassword = (rule, value, callback) => {
  //如果校验通过 直接调用callback()
  //如果校验不通过 callback()里面传参数
  if (value.length < 3) {
    callback("密码至少3位");
  } else {
    callback();
  }
};
//校验验证码
var validateCaptcha = (rule, value, callback) => {
  //如果校验通过 直接调用callback()
  //如果校验不通过 callback()里面传参数
  if (value.length !== 5) {
    callback("验证码必须是五位");
  } else {
    callback();
  }
};

export default {
  components: {
    Common,
    Qr,
    Mess
  },
  data() {
    return {
      //common常规登入  qr二维码登入   mess短信登入
      loginType: "common", //登陆方式
      title: "千峰管理系统",
      loginForm: {
        username: "",
        password: "",
        captcha: ""
      },
      rules: {
        //表单验证规则
        username: [{ validator: validateUsername, trigger: "blur" }],
        password: [{ validator: validatePassword, trigger: "blur" }],
        captcha: [{ validator: validateCaptcha, trigger: "blur" }]
      }
    };
  },
  //计算属性
  computed: {
    jiaobiaoType() {
      if (this.loginType === "common") {
        return "icon-erweimajiaobiao";
      }
      if (this.loginType === "qr" || this.loginType === "mess") {
        return "icon-diannaojiaobiao";
      }
    }
  },
  methods: {
    changeMessType() {
      this.loginType = "mess";
    },
    //更改登入方式
    changeLoginType() {
      if (this.loginType === "common") {
        this.loginType = "qr";
      } else if (this.loginType === "qr" || this.loginType === "mess") {
        this.loginType = "common";
      }
    },
    //表单提交
    submit() {
      this.$refs["loginForm"].validate((valid) => {
        if (valid) {
          //登入
          //1.获取后台的验证码
          //2.进行本地校验
          //3.本地校验通过=>先校验验证码
          //4.验证码通过再校验用户名和密码
          //5.成功或失败
          // alert("submit!");
          api.verifyCaptcha(this.loginForm.captcha).then(async (res) => {
            if (res.data.state) {
              //验证码通过继续校验用户名和密码
              let { username, password } = this.loginForm;
              let res = await api.loginApi(username, password);
              // console.log(res);
              //登入成功
              if (res.data.state) {
                //存储用户信息到本地
                this.storageUserInfo(res.data);
                //跳转到主页
                this.$router.push("/");
              }
            } else {
              //验证码错误，刷新验证码
              this.$refs["commonLogin"].getCaptcha();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //封装一个方法，用于存储用户的信息和token
    storageUserInfo({ userInfo, token, permission }) {
      storage.set("userInfo", userInfo);
      storage.set("vue2token", token);
      storage.set("permission", permission);
      let r = storage.get("userInfo");
      console.log(r);
    },

    // resetForm(formName) {
    //   this.$refs[formName].resetFields() ;
    // },
    handlerLogin(newLogin) {
      this.loginForm = newLogin;
    }
  }
};
</script>

<style lang="less" scoped>
//修改element-ui样式
//1、打开调试器，找准类名，覆盖样式
//2、看权重，使用！important
//3、使用深度选择器 >>> 或 /deep/
.ps {
  /deep/ .el-input__inner {
    width: 300px;
  }
}

.loginForm2 {
  width: 400px;
  height: 500px;
  position: fixed;
  right: 150px;
  top: 120px;
  padding-top: 50px;
  padding-right: 60px;
  background-color: rgba(0, 0, 0, 0.3);
  .title {
    text-align: center;
    margin-left: 50px;
    margin-bottom: 50px;
  }
  .jiaobiao {
    position: relative;
    top: -160px;
    right: -430px;
  }
  /deep/ .icon-erweimajiaobiao {
    font-size: 35px !important;
  }
  /deep/ .icon-diannaojiaobiao {
    font-size: 35px !important;
  }
}
</style>
